package com.bdsys.daoimpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bdsys.common.dao.BasicDaoAbstract;
import com.bdsys.dao.ImageDAO;
import com.bdsys.domain.ImageDomain;

public class ImageDAOImpl extends BasicDaoAbstract implements ImageDAO{
	
	public ImageDAOImpl(){
		super("image");
	}

	@Override
	public void addImage(ImageDomain inDomain) {
		getJdbcTemplate().update(getSql(IMAGE_INSERT),
				new Object[] { inDomain.getDid(), inDomain.getName(),
				inDomain.getContent(), inDomain.getDescription(), inDomain.getDefalut() });
		
	}

	@Override
	public List<ImageDomain> selectByDid(String did) {
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(getSql(IMAGE_SEARCH_BY_DID), new Object[]{did});
		
		List<ImageDomain> imagesInDid = new ArrayList<ImageDomain>(); 
		for(Map<String, Object> map : list){
			ImageDomain image = this.fromMapToDomain(map);
			imagesInDid.add(image);
		}
		return imagesInDid;
	}
	
	public ImageDomain fromMapToDomain(Map<String, Object> map){
		ImageDomain domain = new ImageDomain();
		domain.setDid(String.valueOf(map.get("did")));
		domain.setName(String.valueOf(map.get("name")));
		domain.setContent((byte[])map.get("content"));
		domain.setDescription(String.valueOf(map.get("description")));
		domain.setDefalut(String.valueOf(map.get("isShowImage")));
		return domain;
	}

	@Override
	public void deleteImage(String did) {
		getJdbcTemplate().update(getSql(IMAGE_DELETE), new Object[]{did});
	}



}
